package com.humanminoritygame.model.mock;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import com.humanminoritygame.model.Category;
import com.humanminoritygame.model.Room;
import com.humanminoritygame.model.Subscription;
import com.humanminoritygame.model.SubscriptionRepo;
import com.humanminoritygame.model.User;
import com.humanminoritygame.model.Subscription.State;

public class MockSubscriptionRepo implements SubscriptionRepo{
  private SortedSet<Subscription> subscriptions = new TreeSet<Subscription>();
  
  public MockSubscriptionRepo(){
    
  }
  
  @Override
  public void addSubscription(Subscription subscription) {
    subscriptions.add(subscription);
    subscription.getRoom().addSubscription(subscription);
  }

  @Override
  public Set<Subscription> getAllSubscriptions() {
    return subscriptions;
  }

  @Override
  public Set<Subscription> getRoomActiveSubscriptions(Room room) {
    Set<Subscription> result = new HashSet<Subscription>();
    Iterator<Subscription> i = subscriptions.iterator();
    while(i.hasNext()){
      Subscription s = i.next();
      if(s.getRoom().equals(room)&& s.getState() == State.ACTIVE){
        result.add(s);
      }
    }
    return result;
  }

  @Override
  public Set<Subscription> getRoomSubscriptions(Room room) {
    Set<Subscription> result = new HashSet<Subscription>();
    Iterator<Subscription> i = subscriptions.iterator();
    while(i.hasNext()){
      Subscription s = i.next();
      if(s.getRoom().equals(room)){
        result.add(s);
      }
    }
    return result;
  }

  @Override
  public Set<Subscription> getUserSubscriptions(User user) {
    Set<Subscription> result = new TreeSet<Subscription>();
    for(Subscription subscription : subscriptions){
      if(subscription.getUser().equals(user)){
        result.add(subscription);
      }
    }
    return result;
  }

  @Override
  public boolean isSubscribed(User user, Category category) {
    for(Subscription subscription : subscriptions){
      if(subscription.getUser().equals(user) && subscription.getRoom().getCategory().equals(category)){
        return true;
      }
    }
    return false;
  }

  @Override
  public Subscription getSubscription(User user, Category category) {
    for(Subscription subscription : subscriptions){
      if(subscription.getUser().equals(user) && subscription.getRoom().getCategory().equals(category)){
        return subscription;
      }
    }
    return null;
  }

  @Override
  public Subscription getSubscription(Long subscriptionId) {
    for(Subscription subscription : subscriptions){
      if(subscription.getId() == subscriptionId){
        return subscription;
      }
    }
    return null;
  }

  @Override
  public Set<Subscription> getSubscriptions(Category category) {
    Set<Subscription> result = new TreeSet<Subscription>();
    for(Subscription subscription : subscriptions){
      if(subscription.getRoom().getCategory().equals(category)){
        result.add(subscription);
      }
    }
    return result;
  }

  @Override
  public Set<Subscription> getActiveSubscriptions(User user) {
    Set<Subscription> result = new TreeSet<Subscription>();
    for(Subscription subscription : subscriptions){
      if(subscription.getUser().equals(user) && subscription.isActive()){
        result.add(subscription);
      }
    }
    return result;
  }

  @Override
  public Set<Subscription> getOpenSubscriptions(User user) {
    Set<Subscription> result = new TreeSet<Subscription>();
    for(Subscription subscription : subscriptions){
      if(subscription.getUser().equals(user) && subscription.getRoom().isOpen()){
        result.add(subscription);
      }
    }
    return result;
  }

  @Override
  public Subscription getSubscription(User user, Room room) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Set<Subscription> getClosedSubscriptions(User user) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ArrayList<Subscription> getRoomSubscriptionsOrderByScore(Room room) {
    // TODO Auto-generated method stub
    return null;
  }
}
